﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace PaintApplicationSurface
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class SurfaceWindow1 : SurfaceWindow
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public SurfaceWindow1()
        {
            InitializeComponent();

            // Add handlers for Application activation events
            AddActivationHandlers();
        }


        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        private void ScatterViewItem_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            MessageBox.Show(e.OriginalSource.ToString(), e.Source.ToString());
        }

        private void ScatterViewItem_ContactChanged(object sender, ContactEventArgs e)
        {
           // MessageBox.Show(e.OriginalSource.ToString(), e.Source.ToString());
        }

        private void rect1_ContactDown(object sender, ContactEventArgs e)
        {
            // Set the color on the CurrentColor indicator and on the SurfaceInkCanvas
            IInputElement element = sender as IInputElement;
            //MessageBox.Show(e.Device.ToString());
            Color color = ((SolidColorBrush)((Rectangle)sender).Fill).Color;

            // Black means the user touched the transparent part of the wheel. In that 
            // case, leave the color set to its current value
            if (color != Colors.Black)
            {
                DrawingPadCanvas.DefaultDrawingAttributes.Color = color;
                //  CurrentColor.Fill = new SolidColorBrush(color);
            }

            // Set editing mode to ink
            DrawingPadCanvas.EditingMode = SurfaceInkEditingMode.Ink;

            // Update the button image
            //EditModeButton.Content = DrawButtonImage;

        }

        private void Grid_ContactDown(object sender, ContactEventArgs e)
        {
            if (DrawingPadCanvas.EditingMode == SurfaceInkEditingMode.EraseByPoint)
                DrawingPadCanvas.EditingMode = SurfaceInkEditingMode.Ink;

        }

        private void Erase_ContactDown(object sender, ContactEventArgs e)
        {
            if (DrawingPadCanvas.EditingMode == SurfaceInkEditingMode.Ink)
                DrawingPadCanvas.EditingMode = SurfaceInkEditingMode.EraseByPoint;


        }
    }
}